package com.sky.Task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 0 1 * * ?")
    public void TimeOutOrder(){
        log.info("清理超时订单");
        LocalDateTime time = LocalDateTime.now().minusMinutes(15);
       List<Orders> ordersList=orderMapper.getTimeOutOrder(Orders.PAID,time);
       if(ordersList!=null&&ordersList.size()>0){
           for (Orders orders : ordersList) {
               orders.setStatus(Orders.CANCELLED);
               orders.setCancelReason("订单超时，自动取消");
               orders.setCancelTime(LocalDateTime.now());
               orderMapper.update(orders);
           }
       }
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void deliveryOrder() {
        log.info("清理上一天的派送订单");
        LocalDateTime time = LocalDateTime.now().minusMinutes(60);
        List<Orders> ordersList = orderMapper.getTimeOutOrder(Orders.DELIVERY_IN_PROGRESS, time);
        if(ordersList!=null&&ordersList.size()>0){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.CANCELLED);
                orderMapper.update(orders);
            }
        }

    }
}
